<template>
  <div>
    <el-dialog
      :title="conditionTitle"
      :visible.sync="signDialog"
      width="430px"
      top="50vh"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      custom-class="samples-dialog material-datail-condition"
      :append-to-body="true"
      :before-close="handleClose"
    >
      <el-row class="samples-dialog-box">
        <el-select
          v-if="conditionTitle ==&quot;签约单位&quot;"
          v-model="searchName"
          style="width: 100%;"
          size="small"
          class="sign-select"
          filterable
          clearable
          remote
          multiple
          :remote-method="queryList"
        >
          <el-option
            v-for="item in lists"
            :key="item.companyId"
            :label="item.companyName"
            :value="item.companyName"
          />
        </el-select>
        <el-select
          v-if="conditionTitle !=&quot;签约单位&quot;"
          v-model="searchNameMaterial"
          style="width: 100%;"
          size="small"
          filterable
          class="sign-select"
          clearable
          remote
          multiple
          :remote-method="queryList"
        >
          <el-option
            v-for="item in listsMaterial"
            :key="item.id"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-row>
      <el-row class="samples-dialog-footer samples-mt-20">
        <el-row class="samples-float-right">
          <el-button size="small" type="primary" plain @click="handleClose">取 消</el-button>
          <el-button size="small" type="primary" @click="premit">确 认</el-button>
        </el-row>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import {
  searchJiaYiApi,
  searchJiaYiNextApi
} from '@/api/contractManage/contractMag'
import {
  materialPageListApi,
  materialExportApi,
  queryMaterialNameApi
} from '@/api/contractManage/contractAnalysis'
export default {
  props: {
    initSearchName: {
      type: Array,
      required: true,
      default: () => []
    },
    initListsMaterial: {
      type: Array,
      required: true,
      default: () => []
    },
    conditionTitle: {
      type: String,
      required: true
    },
    detailId: {
      type: String,
      default: '1'
    },
    signDialog: {
      type: Boolean,
      required: false
    }
  },
  data() {
    return {
      lists: [],
      listsMaterial: [],
      searchNameMaterial: [],
      searchName: [],
      signSearchQuery: {
        name: ''
      },
      materielSearchQuery: {
        name: ''
      }
    }
  },
  methods: {
    premit() {
      this.dialogSelectDisP = false
      this.$emit('childSelect-say', this.dialogSelectDisP)
      if (this.conditionTitle == '签约单位') {
        this.$emit('searchName', this.searchName)
      } else {
        this.$emit('materialName', this.searchNameMaterial)
      }
    },
    handleClose() {
      this.dialogSelectDisP = false
      this.$emit('childSelect-say', this.dialogSelectDisP)
      if (this.conditionTitle == '签约单位') {
        this.searchName = this.initSearchName
        this.lists = []
        this.$emit('searchName', this.searchName)
      } else {
        this.searchNameMaterial = this.initListsMaterial
        this.listsMaterial = []
        this.$emit('materialName', this.searchNameMaterial)
      }
    },
    queryList(queryString) {
      if (!queryString) return// 这行极其重要

      if (this.conditionTitle == '签约单位') {
        var query = {
          companyName: queryString
        }
        this.lists = []
        Promise.all([searchJiaYiApi(query), searchJiaYiNextApi(query)]).then(res => {
          if (res[1].code == 1 && res[1].data.data && res[0].code == 1 && res[0].data.data) {
            this.lists = this.lists.concat(res[0].data.data).concat(res[1].data.data)
          } else {
            this.$notify({
              title: '警告',
              type: 'warning',
              message: '查询失败',
              duration: '2000'
            })
          }
        })
      } else {
        queryMaterialNameApi({ name: queryString }).then(res => {
          if (res.code === 1) {
            this.listsMaterial = res.data
            // this.searchNameMaterial = res.data
            //
          } else {
            this.$notify({
              title: '警告',
              type: 'warning',
              message: res.msg,
              duration: '2000'
            })
          }
        })
      }
    }

  }
}
</script>

<style lang="scss" >
    .material-datail-condition {
      .el-dialog__body{
          padding-top: 10px;
      }

    }
</style>
